import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/net.dart';
import 'package:fluttertoast/fluttertoast.dart';

class TaskListPage extends StatefulWidget {
  const TaskListPage({super.key});
  @override
  State<TaskListPage> createState() => _TaskListPageState();
}

class _TaskListPageState extends State<TaskListPage> {
  List titles = ['待提货', '在途中', '已完成'];
  int _currentIndex = 0;
  int page = 0;
  int pageSize = 10;
  int status = 1; // ，1为待提货）、2为在途(在途和已交付)、3为改派、5为已作废、6为已完成（已回车登记）
  List items = [];
  int totalPage = 0;

  getList() async {
    switch (_currentIndex) {
      case 0:
        status = 1;
        break;
      case 1:
        status = 2;
        break;
      case 2:
        status = 6;
        break;
    }
    Map<String, dynamic> params = {};
    params['page'] = page;
    params['pageSize'] = pageSize;
    params['status'] = status;
    try {
      var result = await http.get('/driver/tasks/list', params);
      if (result['code'] == 200) {
        setState(() {
          items = result['data']['items'] ?? [];
          totalPage = int.parse(result['data']['pages']);
        });
      } else {
        Fluttertoast.showToast(msg: '获取列表数据失败');
      }
    } catch (e) {
      Fluttertoast.showToast(msg: '获取列表数据失败$e');
    }
  }

  @override
  void initState() {
    super.initState();

    getList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            TabBar(
              tabs: titles
                  .map(
                    (item) => Padding(
                      padding: EdgeInsets.all(20),
                      child: Text(item, style: TextStyle(fontSize: 20)),
                    ),
                  )
                  .toList(),
              onTap: (value) {
                _currentIndex = value;
                getList();
              },
            ),
            Expanded(
              flex: 1,
              child: ListView.builder(
                itemBuilder: (context, index) {
                  return Padding(
                    padding: EdgeInsets.all(10),
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                Text(
                                  '任务编号：${items[index]['id']}',
                                  style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                Expanded(child: Spacer()),
                                Text('已延迟'),
                              ],
                            ),
                            SizedBox(height: 10),
                            Row(
                              children: [
                                CircleAvatar(
                                  backgroundColor: Colors.black,
                                  child: Text(
                                    '起',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                                SizedBox(width: 10),
                                Text(
                                  '${items[index]['startAddress']}',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Colors.grey,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(height: 10),
                            Row(
                              children: [
                                CircleAvatar(
                                  backgroundColor: Colors.orange,
                                  child: Text(
                                    '止',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                      color: Colors.white,
                                      backgroundColor: Colors.orange,
                                    ),
                                  ),
                                ),

                                SizedBox(width: 10),
                                Text(
                                  '${items[index]['endAddress']}',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Colors.grey,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(height: 10),
                            Row(
                              children: [
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      '提货时间',
                                      style: TextStyle(
                                        fontSize: 18,
                                        color: Colors.grey,
                                      ),
                                    ),
                                    SizedBox(height: 10),
                                    Text(
                                      '${items[index]['planArrivalTime']}',
                                      style: TextStyle(
                                        fontSize: 18,
                                        color: Colors.black,
                                      ),
                                    ),
                                  ],
                                ),
                                Expanded(child: Spacer()),
                                ElevatedButton(
                                  onPressed: () {},
                                  child: Text('提货'),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                },
                itemCount: items.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
